<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import {
  defaultFormData,
  disabledTime,
  formRules,
} from '../index'
import type { CustomDialogInstance, DepartmentalInfo, IndicatorInfo, PointInfo } from '@/types'
import {
  indicatorApi,
  pointApi,
  sysDepartmentalApi,
} from '@/api'

// ------------------use------------------
// #region
defineProps<{
  title: string
}>()
const emits = defineEmits<{
  'save-success': []
}>()
const formData = defineModel('data', {
  default: () => defaultFormData,
})
const { withLoading, vLoading } = useDefineLoading()
const [departmentalData, setDepartmentalData] = useState<
  DepartmentalInfo[]
>([])
const [indicatorData, setIndicatorData] = useState<
  IndicatorInfo[]
>([])
const [pointData, setPointData] = useState<PointInfo[]>([])

// #endregion
// ------------------静态变量------------------
// #region

// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const dialogRef = ref<CustomDialogInstance | null>(null)
const formRef = ref<FormInstance | null>(null)
// #endregion
// ------------------reactive------
// #region
// #endregion
// ------------------computed------
// #region
const computedTime = computed({
  get() {
    return [
      formData.value.startTime,
      formData.value.endTime,
    ]
  },
  set([a, b]: any) {
    formData.value.startTime = a
    formData.value.endTime = b
  },
})
// #endregion
// ------------------生命周期------------------
// #region

// #endregion
// ------------------内置方法---------------------
// #region
// #endregion
// ------------------方法---------------------
// #region
function init() {
  getDepartmentalData()
  getIndicatorData()
  getPointData()
}

async function getDepartmentalData() {
  const { data: res }
    = await sysDepartmentalApi.queryTreeList()
  setDepartmentalData(res)
}
async function getIndicatorData() {
  if (!formData.value.pointId)
    return
  const { data: res } = await indicatorApi.loadPointList(
    formData.value.pointId ?? 0,
  )
  setIndicatorData(res || [])
}
async function getPointData() {
  const { data: res } = await withLoading(
    pointApi.loadRoleEnergyUnit,
  )()
  setPointData(res.list || [])
}
async function open() {
  dialogRef.value?.open?.()
  await nextTick()
  dialogRef.value?.setSaving?.(false)
  formRef.value?.resetFields()
  init()
}
function close() {
  dialogRef.value?.close?.()
}
async function clickSave(done: () => void) {
  try {
    await formRef.value?.validate()
    emits('save-success')
  }
  catch (error) {
    done()
  }
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    :title="title"
    width="600px"
    @confirm="clickSave"
  >
    <el-form
      ref="formRef"
      v-loading
      label-width="80px"
      label-position="right"
      :model="formData"
      :rules="formRules"
      inline
    >
      <el-form-item
        label="日期"
        prop="startTime"
        class="width-fill"
      >
        <el-date-picker
          v-model="computedTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:00"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          time-format="HH:00"
          :disabled-minutes="disabledTime"
        />
      </el-form-item>
      <el-form-item
        label="班组"
        prop="teamsName"
        class="width-fill"
      >
        <el-cascader
          v-model="formData.teamsName"
          :options="departmentalData as any[]"
          :props="{
            value: 'departName',
            label: 'departName',
            checkStrictly: true,
            emitPath: false,
          }"
          class="width-fill"
          clearable
        />
      </el-form-item>
      <el-form-item
        label="点位"
        prop="pointId"
        class="width-fill"
      >
        <el-cascader
          v-model="formData.pointId"
          :options="pointData"
          filterable
          :props="{
            value: 'id',
            label: 'pointName',
            checkStrictly: true,
            emitPath: false,
          }"
          class="width-fill"
          clearable
          @change="getIndicatorData"
        />
      </el-form-item>
      <el-form-item
        v-show="formData.pointId"
        label="指标"
        prop="indicatorCode"
        class="width-fill"
      >
        <el-select
          v-model="formData.indicatorCode"
          class="width-fill"
        >
          <el-option
            v-for="item in indicatorData"
            :key="item.id"
            :value="item.indicatorCode"
            :label="item.indicatorName"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
